package com.tiger.admin.http.secrecy;

import com.tiger.admin.http.common.member.MemberService;
import com.tiger.admin.http.encipher.EncipherService;
import com.tiger.utils.ResponseUtil;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.UUID;

@RestController
@RequestMapping("secrecy")
public class SecrecyController {
    private final EncipherService encipherService;
    private final MemberService memberService;

    public SecrecyController(EncipherService encipherService, MemberService memberService) {
        this.encipherService = encipherService;
        this.memberService = memberService;
    }

    @PostMapping("auth")
    public Object auth(@RequestBody() HashMap<String, String> data, @RequestAttribute("memberId") long memberId, HttpSession session) throws Exception {
        String password = encipherService.decrypt(session, data.get("password"));
        if(password == null) return ResponseUtil.failRes("密码错误");

        if(!memberService.checkPassword(password, memberId)) return ResponseUtil.failRes("密码错误");

        String uuid = UUID.randomUUID().toString();

        session.setAttribute(uuid, 1);

        return ResponseUtil.successRes(uuid);

    }
}
